function postToUrl(path, params, method) {
	method = method || "post";

	var form = document.createElement("form");
	form.setAttribute("method", method);
	form.setAttribute("action", path);

	for ( var key in params) {
		var hiddenField = document.createElement("input");
		hiddenField.setAttribute("type", "hidden");
		hiddenField.setAttribute("name", key);
		hiddenField.setAttribute("value", params[key]);

		form.appendChild(hiddenField);
	}

	document.body.appendChild(form);
	form.submit();
}

function toggleBox(boxId, dataTag, display) {

	// alert(boxId + ', ' + dataTag + ', ' + display);
	var boxImage = document.getElementById(boxId);

	if (boxImage) {

		// alert('current source: ' + boxImage.src);

		if (/expand.gif$/.test(boxImage.src)) {
			boxImage.src = 'img/collapse.gif';
		} else {
			boxImage.src = 'img/expand.gif';
		}

		// } else {
		// alert('no element for ' + boxId);

	}

	toggleDisplay(dataTag, display)

}

function toggleDisplay(dataTag, display) {
	var dataElement = document.getElementById(dataTag);

	if (display) {

		dataElement.style.display = display;

	} else {

		if (dataElement.style.display == 'none'
				|| dataElement.style.display == '') {
			dataElement.style.display = 'block';
		} else {
			dataElement.style.display = 'none';
		}

	}
}

function loadUsers() {
	var params = {
		"action" : 'loadusers'
	};
	postToUrl('main', params, 'get');
}

function deleteUser(userId) {

	if (!confirm('delete ' + userId + '?'))
		return;

	var params = {
		"action" : 'deleteuser',
		"user_id" : userId
	};
	postToUrl('main', params, 'get');
}

function loadGroups() {
	var params = {
		"action" : 'loadgroups'
	};
	postToUrl('main', params, 'get');
}

function deleteGroup(groupId) {

	if (!confirm('delete ' + groupId + '?'))
		return;

	var params = {
		"action" : 'deletegroup',
		"group_id" : groupId
	};
	postToUrl('main', params, 'get');
}

function loadGroupMembers(groupId) {

	/* disable member button */
	var memberButton = document.getElementById('member_button_' + groupId);
	memberButton.disabled = true;

	xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			// var responseText = xmlhttp.responseText;
			// alert(responseText);

			var response = xmlhttp.responseXML.documentElement;

			// get response data
			try {
				var messages = response.getElementsByTagName('message');
				if (messages.length > 0) {
					alert(messages[0].firstChild.data);
				}

				/* get group id */
				var responseGroupId;
				var groupIdElement = response.getElementsByTagName('group_id');
				if (groupIdElement.length > 0) {
					responseGroupId = groupIdElement[0].firstChild.nodeValue;

					/* activate member button */
					var memberButton = document.getElementById('member_button_'
							+ responseGroupId);
					memberButton.disabled = false;

				}

				var memberList = document.getElementById('group_member_list_'
						+ responseGroupId);
				removeChildNodes(memberList);

				var members = response.getElementsByTagName('member');

				if (members.length == 0) {

					var memberEntry = document.createElement('div');
					memberEntry.appendChild(document
							.createTextNode('EMPTY LIST'));
					memberList.appendChild(memberEntry);

				} else {

					for (index = 0; index < members.length; index++) {
						var member = members[index];

						var memberId = getNodeValue(member, 'memberId');
						// var memberType = getNodeValue(member, 'memberType');
						// var directMember = getNodeValue(member,
						// 'directMember');

						/* member entry element */
						var memberEntry = document.createElement('div');
						memberEntry.className = 'member_entry';
						memberEntry.appendChild(document
								.createTextNode(memberId));

						/* append entry to member list */
						memberList.appendChild(memberEntry);

					}

				}

			} catch (e) {

				alert(e);

			}

		}
	};

	xmlhttp.open('POST', 'groupmembers', true);
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlhttp.overrideMimeType("text/xml");
	xmlhttp.send('group=' + encodeURI(groupId));

}

function loadGroupOwners(groupId) {

	/* disable owner button */
	var ownerButton = document.getElementById('owner_button_' + groupId);
	ownerButton.disabled = true;

	xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			// var responseText = xmlhttp.responseText;
			// alert(responseText);

			var response = xmlhttp.responseXML.documentElement;

			// get response data
			try {
				var messages = response.getElementsByTagName('message');
				if (messages.length > 0) {
					alert(messages[0].firstChild.data);
				}

				/* get group id */
				var responseGroupId;
				var groupIdElement = response.getElementsByTagName('group_id');
				if (groupIdElement.length > 0) {
					responseGroupId = groupIdElement[0].firstChild.nodeValue;

					/* activate owner button */
					var ownerButton = document.getElementById('owner_button_'
							+ responseGroupId);
					ownerButton.disabled = false;

				}

				var ownerList = document.getElementById('group_owner_list_'
						+ responseGroupId);
				removeChildNodes(ownerList);

				var owners = response.getElementsByTagName('owner');

				if (owners.length == 0) {

					var ownerEntry = document.createElement('div');
					ownerEntry.appendChild(document
							.createTextNode('EMPTY LIST'));
					ownerList.appendChild(ownerEntry);

				} else {

					for (index = 0; index < owners.length; index++) {
						var owner = owners[index];

						var ownerId = getNodeValue(owner, 'memberId');
						// var memberType = getNodeValue(owner, 'memberType');
						// var directMember = getNodeValue(owner,
						// 'directMember');

						/* owner entry element */
						var ownerEntry = document.createElement('div');
						ownerEntry.className = 'owner_entry';
						ownerEntry
								.appendChild(document.createTextNode(ownerId));

						/* append entry to owner list */
						ownerList.appendChild(ownerEntry);

					}

				}

			} catch (e) {

				alert(e);

			}

		}
	};

	xmlhttp.open('POST', 'groupowners', true);
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlhttp.overrideMimeType("text/xml");
	xmlhttp.send('group=' + encodeURI(groupId));

}

function loadNicknames(userId) {

	/* disable nickname button */
	var nicknameButton = document.getElementById('nickname_button_' + userId);
	nicknameButton.disabled = true;

	xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			// var responseText = xmlhttp.responseText;
			// alert(responseText);

			var response = xmlhttp.responseXML.documentElement;

			// get response data
			try {
				var messages = response.getElementsByTagName('message');
				if (messages.length > 0) {
					alert(messages[0].firstChild.data);
				}

				/* get user id */
				var responseUserId;
				var userIdElement = response.getElementsByTagName('user_id');
				if (userIdElement.length > 0) {
					responseUserId = userIdElement[0].firstChild.nodeValue;

					/* activate nickname button */
					var nicknameButton = document
							.getElementById('nickname_button_' + responseUserId);
					nicknameButton.disabled = false;

				}

				var nicknameList = document.getElementById('nickname_list_'
						+ responseUserId);
				removeChildNodes(nicknameList);

				var nicknames = response.getElementsByTagName('nickname');

				if (nicknames.length == 0) {

					var nicknameEntry = document.createElement('div');
					nicknameEntry.appendChild(document
							.createTextNode('EMPTY LIST'));
					nicknameList.appendChild(nicknameEntry);

				} else {

					for (index = 0; index < nicknames.length; index++) {
						var nickname = nicknames[index];

						/* nickname entry element */
						var nicknameEntry = document.createElement('div');
						nicknameEntry.className = 'nickname_entry';
						nicknameEntry.appendChild(document
								.createTextNode(nickname.firstChild.nodeValue));

						/* append entry to nickname list */
						nicknameList.appendChild(nicknameEntry);

					}

				}

			} catch (e) {

				alert(e);

			}

		}
	};

	xmlhttp.open('POST', 'nickname', true);
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlhttp.overrideMimeType("text/xml");
	xmlhttp.send('user=' + encodeURI(userId));

}

function loadSendAs(userId) {

	/* disable sendas button */
	var sendasButton = document.getElementById('sendas_button_' + userId);
	sendasButton.disabled = true;

	xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			// var responseText = xmlhttp.responseText;
			// alert(responseText);

			var response = xmlhttp.responseXML.documentElement;

			// get response data
			try {
				var messages = response.getElementsByTagName('message');
				if (messages.length > 0) {
					alert(messages[0].firstChild.data);
				}

				/* get user id */
				var responseUserId;
				var userIdElement = response.getElementsByTagName('user_id');
				if (userIdElement.length > 0) {
					responseUserId = userIdElement[0].firstChild.nodeValue;

					/* activate send as button */
					var sendasButton = document.getElementById('sendas_button_'
							+ responseUserId);
					sendasButton.disabled = false;

				}

				var sendasList = document.getElementById('sendas_list_'
						+ responseUserId);
				removeChildNodes(sendasList);

				var sendaslist = response.getElementsByTagName('sendas');

				if (sendaslist.length == 0) {

					var sendasEntry = document.createElement('div');
					sendasEntry.appendChild(document
							.createTextNode('EMPTY LIST'));
					sendasList.appendChild(sendasEntry);

				} else {

					for (index = 0; index < sendaslist.length; index++) {
						var sendas = sendaslist[index];

						/* nickname entry element */
						var sendasEntry = document.createElement('div');
						sendasEntry.className = 'sendas_entry';
						sendasEntry.appendChild(document
								.createTextNode(sendas.firstChild.nodeValue));

						/* append entry to send as list */
						sendasList.appendChild(sendasEntry);

					}

				}

			} catch (e) {

				alert(e);

			}

		}
	};

	xmlhttp.open('POST', 'sendas', true);
	xmlhttp.setRequestHeader("Content-type",
			"application/x-www-form-urlencoded");
	xmlhttp.overrideMimeType("text/xml");
	xmlhttp.send('user=' + encodeURI(userId));

}

function removeChildNodes(ctrl) {
	while (ctrl.childNodes[0]) {
		ctrl.removeChild(ctrl.childNodes[0]);
	}
}

function getNodeValue(obj, tag) {
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

function setLanguage(lang) {
	var params = {
		"lang" : lang
	};
	postToUrl('main', params, 'get');
}
